Problem Set - Integer Operations and Overflow
Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Binary Arithmetic #◇Overflow Flag #◇Carry Flag #◇ALU #◇Two's Complement
◇Overview
In digital arithmetic and processor microarchitecture (◇ALU), integer addition and subtraction are executed on fixed-width binary words (typically , , , or bits). Because physical hardware registers possess a finite bit width , arithmetic operations can produce results that exceed the storage capacity of the destination register.
Microprocessors track boundary conditions by asserting specific condition status flags in the Program Status Word / Flags register:
- Carry Flag ( /
CF): Indicates carry-out or borrow during unsigned integer arithmetic. - Overflow Flag ( /
OF): Indicates mathematical magnitude overflow during signed 2's complement arithmetic. - Sign Flag ( / /
SF): Mirrors the Most Significant Bit (MSB) of the result (). - Zero Flag ( /
ZF): Asserted () when all bits of the result equal .
┌─────────────────────────────────────────┐
│ 8-bit ALU Arithmetic Result │
└────────────────────┬────────────────────┘
│
┌─────────────────────────────┴─────────────────────────────┐
▼ ▼
┌─────────────────────────┐ ┌─────────────────────────┐
│ Unsigned Evaluation │ │ Signed Evaluation │
│ Range: [0, 255] │ │ Range: [-128, +127] │
└────────────┬────────────┘ └────────────┬────────────┘
│ │
┌──────────┴──────────┐ ┌──────────┴──────────┐
▼ ▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ C_out == 0 │ │ C_out == 1 │ │ C_in == │ │ C_in != │
│ (Addition) │ │ (Addition) │ │ C_out │ │ C_out │
│ NO OVF │ │ OVERFLOW │ │ NO OVF │ │ OVERFLOW │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
◇Theoretical Foundations
1. Hardware Subtraction via 2's Complement Addition
Digital subtractors avoid dedicated subtraction logic by transforming subtraction into addition using radix complement properties:
where represents the bitwise inversion (1's complement) of , and is injected via the initial carry-in () of the full-adder chain.
2. Unsigned Carry vs. Signed Overflow Rules
| Feature | Unsigned Integer Arithmetic | Signed (2's Complement) Arithmetic |
|---|---|---|
| Valid Range () | ||
| Addition Overflow | Occurs when true sum () | Occurs when true sum |
| Subtraction Overflow | Occurs when (requires a borrow: ) | Occurs when |
| Hardware Detection | Carry Flag (CF): (for addition) | Overflow Flag (OF): |
| Sign-Logic Formula | N/A (operands are strictly non-negative) |
[!important] The MSB Rule
In any -bit 2's complement adder, signed overflow occurs if and only if the carry-in into the most significant bit () differs from the carry-out from the most significant bit ():
◇Problem 1: 8-Bit Binary Addition
Problem Statement
Given two 8-bit integers, perform the binary addition. The result must be represented as an 8-bit binary value. Determine whether an overflow occurs when the result is interpreted as an unsigned integer versus a signed integer.
Step-by-Step Solution
1. Binary Column Addition & Bitwise Carry Propagation
We set up the 8-bit full-adder chain from bit (LSB) to bit (MSB):
-
Bit-by-Bit Evaluation:
- Bit 0:
- Bit 1:
- Bit 2:
- Bit 3:
- Bit 4:
- Bit 5:
- Bit 6:
- Bit 7 (MSB):
-
Critical Boundary Carries:
- Carry-in into MSB ():
- Carry-out from MSB ():
- Result: ()
2. Overflow Analysis
Perspective A: Unsigned Integer Interpretation
- Operand Values: ,
- True Mathematical Sum:
- 8-Bit Unsigned Dynamic Range:
- Verification: Since , the mathematical sum fits entirely inside the 8-bit unsigned register.
- Hardware Flag: Carry-out .
- Conclusion: No Overflow
Perspective B: Signed (2's Complement) Integer Interpretation
- Operand Values: (MSB ), (MSB )
- True Mathematical Sum:
- 8-Bit Signed Dynamic Range:
- Verification: , which exceeds the maximum positive limit of an 8-bit signed integer. The resulting bit pattern has MSB , which decodes to:
Adding two positive integers produced an illogical negative result. - Hardware Flag Verification:
- Conclusion: Yes (Overflow Occurred)
Solution Summary Table (Problem 1)
| Field | Value |
|---|---|
| Operation | |
| 8-Bit Result | |
| If viewed as unsigned integer | No (Sum , ) |
| If viewed as signed integer | Yes (Sum , ) |
◇Problem 2: 8-Bit Binary Subtraction
Problem Statement
Given two 8-bit integers, perform the binary subtraction. The result must be represented as an 8-bit binary value. Determine whether an overflow occurs when the result is interpreted as an unsigned integer versus a signed integer.
Step-by-Step Solution
1. Transformation to 2's Complement Addition
Let:
- Minuend
- Subtrahend (unsigned) or (signed 2's complement)
To execute , the ALU negates into its 2's complement representation () and performs addition:
- Bitwise Inversion (1's complement):
- Add (2's complement):
2. Binary Column Addition of
-
Bit-by-Bit Evaluation:
- Bit 0:
- Bit 1:
- Bit 2:
- Bit 3:
- Bit 4:
- Bit 5:
- Bit 6:
- Bit 7 (MSB):
-
Critical Boundary Carries:
- Carry-in into MSB ():
- Carry-out from MSB ():
- Result: ()
3. Overflow Analysis
Perspective A: Unsigned Integer Interpretation
- Operand Values: ,
- True Mathematical Difference:
- 8-Bit Unsigned Dynamic Range:
- Underflow / Borrow Detection:
- Because , a negative value cannot be represented as an unsigned integer.
- In unsigned subtraction , an underflow (borrow) occurs whenever .
- Hardware mapping: .
- Since , , indicating an unsigned overflow/underflow condition. The resulting bit pattern evaluates to ().
- Conclusion: Yes (Overflow / Borrow Occurred)
Perspective B: Signed (2's Complement) Integer Interpretation
- Operand Values:
- (positive)
- (negative, because )
- True Mathematical Difference:
- 8-Bit Signed Dynamic Range:
- Verification:
- , which exceeds the maximum positive 8-bit limit.
- Subtracting a negative integer from a positive integer mathematically must yield a positive integer.
- The resulting 8-bit pattern has MSB , yielding in 2's complement.
- Hardware Flag Verification:
- Conclusion: Yes (Overflow Occurred)
Solution Summary Table (Problem 2)
| Field | Value |
|---|---|
| Operation | |
| 8-Bit Result | |
| If viewed as unsigned integer | Yes (Difference , ) |
| If viewed as signed integer | Yes (Difference , ) |
◇Key Takeaways & Master Reference Guide
Flag Computation Matrix (-bit Arithmetic)
┌──────────────────────────────────────────────┐
│ ALU Status Flag Formulas │
├──────────────────────────────────────────────┤
│ Zero Flag: Z = ~(S_0 | S_1 | ... | S_7) │
│ Sign Flag: S = S_7 │
│ Carry Flag: C = C_8 (Add) or ~C_8 (Sub) │
│ Overflow Flag: V = C_7 ^ C_8 │
└──────────────────────────────────────────────┘
Unsigned vs. Signed Overflow Quick-Check Rules
-
Unsigned Arithmetic:
- Addition (): Overflow occurs ().
- Subtraction (): Overflow (Borrow) occurs (, resulting in negative difference).
-
Signed 2's Complement Arithmetic:
- Addition ():
-
- Subtraction ():
-
- Universal Hardware Check:
- Universal Hardware Check:
◇References & Navigation
- Related Notes: ◇Binary Addition, Subtraction & Carry Lookahead Adders